

package com.io.controller;

import com.io.dto.ApiResult;
import com.io.entity.SysDictEntity;
import com.io.service.SysDictService;
import com.io.utils.PageUtils;
import com.io.utils.validator.ValidatorUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

/**
 * 数据字典
 *
 * @author guodd
 */
@Api(tags = "数据字典")
@RestController
@RequestMapping("sys/dict")
public class SysDictController {
    @Autowired
    private SysDictService sysDictService;

    @ApiOperation("字典列表")
    @PostMapping("/list")
    @RequiresPermissions("sys:dict:list")
    public ApiResult list(@RequestParam Map<String, Object> params) {
        PageUtils page = sysDictService.queryPage(params);
        return ApiResult.ok(page.getList()).put("total", page.getTotalCount());
    }

    @ApiOperation("字典列表信息")
    @GetMapping("/info/{id}")
    @RequiresPermissions("sys:dict:info")
    public ApiResult info(@PathVariable("id") Long id) {
        SysDictEntity dict = sysDictService.getById(id);
        return ApiResult.ok(dict);
    }

    @ApiOperation("保存字典")
    @PostMapping("/save")
    @RequiresPermissions("sys:dict:save")
    public ApiResult save(@RequestBody SysDictEntity dict) {
        //校验类型
        ValidatorUtils.validateEntity(dict);
        sysDictService.save(dict);
        return ApiResult.ok();
    }

    @ApiOperation("修改字典")
    @PostMapping("/update")
    @RequiresPermissions("sys:dict:update")
    public ApiResult update(@RequestBody SysDictEntity dict) {
        //校验类型
        ValidatorUtils.validateEntity(dict);
        sysDictService.updateById(dict);
        return ApiResult.ok();
    }

    @ApiOperation("删除字典")
    @PostMapping("/delete")
    @RequiresPermissions("sys:dict:delete")
    public ApiResult delete(@RequestBody Long[] ids) {
        sysDictService.removeByIds(Arrays.asList(ids));
        return ApiResult.ok();
    }

}
